<?php

namespace App\Http\Controllers;

use App\Models\OperationLog;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class OperationLogController extends Controller
{
    /**
     * 显示操作日志列表
     */
    public function index(Request $request)
    {
        $query = OperationLog::with(['user'])
            ->orderBy('created_at', 'desc');

        // 搜索条件
        if ($request->filled('user_id')) {
            $query->where('user_id', $request->user_id);
        }
        if ($request->filled('action')) {
            $query->where('action', $request->action);
        }
        if ($request->filled('target_type')) {
            $query->where('target_type', $request->target_type);
        }
        if ($request->filled('date_start')) {
            $query->whereDate('created_at', '>=', $request->date_start);
        }
        if ($request->filled('date_end')) {
            $query->whereDate('created_at', '<=', $request->date_end);
        }

        $logs = $query->paginate(10);
        $users = User::all();
        $actions = OperationLog::distinct()->pluck('action');
        $targetTypes = OperationLog::distinct()->pluck('target_type');

        return view('logs.index', compact('logs', 'users', 'actions', 'targetTypes'));
    }

    /**
     * 导出操作日志
     */
    public function export(Request $request)
    {
        $query = OperationLog::with(['user'])
            ->orderBy('created_at', 'desc');

        // 搜索条件
        if ($request->filled('user_id')) {
            $query->where('user_id', $request->user_id);
        }
        if ($request->filled('action')) {
            $query->where('action', $request->action);
        }
        if ($request->filled('target_type')) {
            $query->where('target_type', $request->target_type);
        }
        if ($request->filled('date_start')) {
            $query->whereDate('created_at', '>=', $request->date_start);
        }
        if ($request->filled('date_end')) {
            $query->whereDate('created_at', '<=', $request->date_end);
        }

        $logs = $query->get();
        
        $headers = [
            'Content-Type' => 'text/csv',
            'Content-Disposition' => 'attachment; filename="operation_logs.csv"',
        ];

        $callback = function() use ($logs) {
            $file = fopen('php://output', 'w');
            
            // 添加 UTF-8 BOM
            fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
            
            // 添加表头
            fputcsv($file, ['ID', '用户', '操作类型', '目标类型', '目标ID', '操作内容', 'IP地址', '时间']);
            
            // 添加数据
            foreach ($logs as $log) {
                fputcsv($file, [
                    $log->id,
                    $log->user ? $log->user->username : '已删除用户',
                    $log->action,
                    $log->target_type,
                    $log->target_id,
                    $log->content,
                    $log->ip,
                    $log->created_at
                ]);
            }
            
            fclose($file);
        };

        return response()->stream($callback, 200, $headers);
    }
} 